home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / smtp / smtpd.4.1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-02  |  2.5 KB  |  137 lines

  1. /* Server SMTP daemon */
  2.  
  3. #include "sys/types.h"
  4. #include "signal.h"
  5. #include "netlib.h"
  6. #include "con.h"
  7. #include <stdio.h>
  8.  
  9. char *Channel;
  10. char *Server;
  11. int  Port;
  12. int  Maxconnections = 4;
  13. char *Network;
  14. int numconnections = 0;
  15.  
  16. extern int errno;
  17.  
  18. struct con openparam;
  19.  
  20. main (argc, argv)
  21. int argc;
  22. char **argv;
  23. {
  24.     register  netfid;
  25.     struct netstate statparam;
  26.     char *us, *them;
  27.     char *getuc ();
  28.     int i;
  29.     int pid;
  30.     int alarmtrap() ;
  31.  
  32.     if (argc != 6)
  33.     {
  34.         printf ("Usage:  %s server channels port maxconns network\n", argv[0]);
  35.         exit(1);
  36.     }
  37.  
  38.     Server = argv[1];
  39.     Channel = argv[2];
  40.     Port = atoi(argv[3]);
  41.     Maxconnections = atoi(argv[4]);
  42.     Network = argv[5];
  43.  
  44.     signal (SIGALRM, alarmtrap);
  45.  
  46.     dup2 (1, 2);            /* make log file standard error */
  47.     while (1)
  48.     {
  49.         openparam.c_mode = CONTCP;
  50.         openparam.c_lport = Port;
  51.         openparam.c_rbufs = 2;    /* Ask for 2K receive buffer */
  52.         if (isbadhost(openparam.c_lcon = gethost(Network)))
  53.             {
  54.             printf ("SMTP daemon cannot find network '%s'", Network);
  55.             exit (1);
  56.             }
  57.  
  58.         while ((netfid = open ("/dev/net/net", &openparam)) < 0)
  59.             {
  60.             printf ("Return of %d from open, errno = %d\n", netfid, errno); 
  61.             fflush (stdout);
  62.             sleep (60);
  63.             }
  64.  
  65.         ioctl (netfid, NETSETE, 0);
  66.  
  67. /*        ioctl (netfid, NETGETS, &statparam);
  68.  
  69.         printf ("Connection from %s\n", hostfmt(openparam.c_fcon,1));
  70. */
  71.         if(( pid = fork()) < 0 ) {
  72. /*            ll_log( "could not fork (%d)", errno);  */
  73.             (void) close( netfid );
  74.         } else if( pid == 0 ) {
  75.             /*
  76.              *  Child
  77.              */
  78.  
  79. /*            us = getuc (thisname());    /* who are we? */
  80.             us = getuc (Network);  /* daemon knows correct name */
  81.             ioctl (netfid, NETGETS, &statparam);
  82.             them = hostname (statparam.n_fcon); /* who are they? */
  83.             dup2 (netfid, 0);
  84.             dup2 (netfid, 1);
  85.             close (netfid);
  86.             execl (Server, Server, them, us, Channel, (char *)0);
  87.             exit (1);
  88.         }
  89.  
  90.  
  91.         /*
  92.          *  Parent
  93.          */
  94.         close (netfid);
  95.         numconnections++;
  96.  
  97.         /*
  98.          *  This code collects ZOMBIES and implements load
  99.          *  limiting by staying in the do loop while the
  100.          *  Maxconnections active.
  101.          */
  102.         if( numconnections > 1 || Maxconnections == 1 )
  103.             do {
  104.                 alarm( 2 );
  105.                 if( wait( &i ) > 0 )
  106.                     numconnections--;
  107.                 alarm( 0 );
  108.             } while( numconnections >= Maxconnections );
  109.  
  110.     }
  111.  
  112. }
  113.  
  114. /*
  115.  * Uppercase a string in place. Return pointer to
  116.  * null at end.
  117.  */
  118. char *
  119. getuc(s)
  120.     char *s;
  121. {
  122.     register char *p,
  123.           c;
  124.     for (p = s; c = *p; p++)
  125.     {
  126.     if (c <= 'z' && c >= 'a')
  127.         *p -= ('a' - 'A');
  128.     }
  129.     return(s);
  130. }
  131.  
  132. alarmtrap ()
  133. {
  134.  
  135.     signal (SIGALRM, alarmtrap);
  136. }
  137.